class Producto < ActiveRecord::Base
  TIPO_TYPES = ["Franela","Franelilla Cuello Redondo",
                "Franela Cuello V", "Interior", "Boxer", 
                "Bikini","Panties", "Medias Altas", 
                "Medias Tobillera"]
  TALLAS_TYPES = ["S","M","L","XL"]
  DEPARTAMENTO_TYPES = ["Infantes", "Ni\u00F1as", "Ni\u00F1os",
                        "Damas", "Caballeros", "Unisex"]

  ##default_scope :order => 'nombre'

  has_many :linea_articulos
  before_destroy :ensure_not_referenced_by_any_line_item

  has_attached_file :photo, :styles => {:medium => "300x300>", 
                                        :thumb => "100x100>"}

  validates :nombre, :descripcion, :photo, :talla, :tipo,
            :departamento, :presence => true
  validates :nombre, :uniqueness => true
  validates :nombre, :length => {:minimum => 10}
  validates :precio, 
            :numericality => {:greater_than_or_equal_to => 0.1}
  validates :tipo, :inclusion => TIPO_TYPES
  validates :talla, :inclusion => TALLAS_TYPES 
  validates :departamento, :inclusion => DEPARTAMENTO_TYPES 
  validates_attachment_content_type :photo,
                                    :content_type=>['image/jpeg',
                                                    'image/png']

  private

    # ensure that there are no line items referencing this product
    def ensure_not_referenced_by_any_line_item
      if linea_articulos.empty?
        return true
      else
        errors.add(:base, 'Line Items present')
        return false
      end
    end
  
end
